package Reminder.dao.impl;

import com.alibaba.fastjson.JSONArray;
import Reminder.dao.RemindDao;
import Reminder.domain.Message;
import Reminder.domain.Utils;

import java.io.IOException;
import java.text.ParseException;

public class RemindDaoImpl implements RemindDao {
    @Override
    public Message setRemindData( int userId,JSONArray remindList) {
        Message msg = Utils.setTXT("data/remind/"+ userId + ".txt",remindList.toString());
        if(msg.toJSON().getString("status").equals("success")){
            return new Message("setRemindData","存储成功","success");
        }
        else{
            return new Message("setRemindData","失败成功","success");
        }

    }

    @Override
    public Message getRemindData(int userId) throws IOException, ParseException {
        Message msg = Utils.loadTXT("data/remind/" + userId + ".txt");
        if(msg.toJSON().getString("status").equals("success")){
            return new Message("getRemindData",msg.toJSON().getString("data"),"success");
        }
        else{
            Utils.setTXT("data/remind/" + userId + ".txt",new JSONArray().toString());
            msg = Utils.loadTXT("data/remind/" + userId + ".txt");
            if(msg.toJSON().getString("status").equals("success")){
                return new Message("getRemindData",msg.toJSON().getString("data"),"success");
            }
            else{
                return new Message("getRemindData","获取提醒信息成功失败","error");
            }

        }
    }

}
